package com.kerrykidz.scmstock.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.common.service.ICommonService;
import com.kerrykidz.scminvoicing.dao.model.GblScmPrdEntity;
import com.kerrykidz.scminvoicing.service.IScmInvocingProductInfoService;
import com.kerrykidz.scmstock.service.IStockQueryService;
import com.kerrykidz.system.dao.model.GblSecBranEntity;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;
import com.rdp.framework.exception.RDPException;

/**
 * 
 * @ClassName: StockQueryController
 * @description: 入库查询控制类
 * @author: huanjiang
 * @date: 2014年8月21日 下午2:39:43
 * @version: V1.0
 *  
 */
@RequestMapping("StockQuery")
@Controller
public class StockQueryController {

	@Autowired
	private IStockQueryService iStockQueryService;

	@Autowired
	private IScmInvocingProductInfoService productService;

	@Autowired
	private ICommonService commonService;

	/**
	 * 
	 * 
	 * @Title: findProductListForStock
	 * @Description: 提供仓储管理-库存查询所需数据
	 * @param: <p>
	 * @param entity
	 * @param: <p>
	 * @param session
	 * @param: <p>
	 * @return
	 * @param: <p>
	 * @throws RDPException
	 *             <p>
	 * @date: 2014年8月21日
	 * @return: GblScmPrdEntity
	 * @throws
	 * 
	 */
	@RequestMapping(value = "findProductListForStock", method = RequestMethod.POST)
	@ResponseBody
	public GblScmPrdEntity findProductListForStock(GblScmPrdEntity entity,
			HttpSession session) throws RDPException {
		List<String> idList = entity.getSelectId();
		if (idList != null && idList.size() == 0) {
			entity.setSelectId(null);
		}
		return this.iStockQueryService.findProductListForStock(entity);
	}

	/**
	 * 
	 * 
	 * @Title: findProductListForStock
	 * @Description: 提供库存盘点-盘点时显示已盘点的商品信息
	 * @param: <p>
	 * @param entity
	 * @param: <p>
	 * @param session
	 * @param: <p>
	 * @return
	 * @param: <p>
	 * @throws RDPException
	 *             <p>
	 * @date: 2014年8月21日
	 * @return: GblScmPrdEntity
	 * @throws
	 * 
	 */
	@RequestMapping(value = "findProductListForTake", method = RequestMethod.POST)
	@ResponseBody
	public List<GblScmPrdEntity> findProductListForTake(GblScmPrdEntity entity,
			@RequestParam(value = "fkTskId", required = false) String fkTskId)
			throws RDPException {
		String branId = UserUtil.getUserFromSession().getBranId();
		entity.setFkBranId(branId);
		List<String> idList = entity.getSelectId();
		if (idList != null && idList.size() == 0) {
			entity.setSelectId(null);
		}
		return this.iStockQueryService.findProductListForTake(entity, fkTskId);
	}

	/**
	 * @throws RDPException
	 * 
	 * 
	 * @Title: toStockQuery
	 * @Description: 提供跳转到库存查询模块
	 * @param: <p>
	 * @return <p>
	 * @date: 2014年8月21日
	 * @return: String
	 * @throws
	 * 
	 */
	@RequestMapping("toStockQuery")
	public ModelAndView toStockQuery() throws RDPException {
		ModelAndView mav = new ModelAndView("stock/stock_query");
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (userInfo != null) {
			String brandId = userInfo.getBranId();
			List<GblSecBranEntity> branList = null;
			if (brandId.toUpperCase().indexOf("ZB") != -1)// 总部
			{
				branList = commonService.getBranListForStock(null);
				mav.addObject("branList", branList);
				for (GblSecBranEntity gblSecBranEntity : branList) {
					brandId = gblSecBranEntity.getPkBranId();
					if (brandId.toUpperCase().indexOf("WX") != -1) {
						mav.addObject("branId", brandId);
					}
				}
			} else {
				branList = commonService.getBranListForStock(brandId);
				mav.addObject("branId", brandId);
				mav.addObject("branList", branList);
			}
		}
		return mav;
	}

}
